package br.com.comanda.util;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import br.com.comanda.vos.Board;
import br.com.comanda.vos.Item;
import br.com.comanda.vos.Product;

public class JSONToVO {

	public static Board toBoard(String boardString) throws JSONException {
		JSONObject boardJson = new JSONObject(boardString);
		Board board = new Board();
		board.setId(boardJson.getInt("id"));
		board.setBoard(boardJson.getString("board"));
		return board;
	}

	public static Item toItem(String itemString) throws JSONException {
		JSONObject itemJSON = new JSONObject(itemString);
		Item item = new Item();
		item.setId(itemJSON.getInt("id"));
		item.setQuantity(itemJSON.getInt("quantity"));
		item.setProduct(toProduct(itemJSON.getString("product")));
		item.setTotal(itemJSON.getDouble("total"));
		return item;
	}

	public static Product toProduct(String productString) {
		Product product = new Product();
		try {
			JSONObject productJSON = new JSONObject(productString);

			product.setDescription(productJSON.getString("description"));
			product.setId((long) productJSON.getInt("id"));
			product.setName(productJSON.getString("name"));
			product.setPrice((float) productJSON.getDouble("price"));

		} catch (Exception e) {
			Log.i("ERROR", e.getMessage());
		}

		return product;
	}

}
